package org.nnxy.foodboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.nnxy.foodboot.entity.FoodCategory1;
import org.nnxy.foodboot.entity.FoodCategory2;
import org.nnxy.foodboot.service.FoodCategory1Service;
import org.nnxy.foodboot.service.FoodCategory2Service;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 食材分类控制器
 */
@RestController
@RequestMapping("/api/category")
@RequiredArgsConstructor
public class FoodCategoryController {
    
    private final FoodCategory1Service category1Service;
    private final FoodCategory2Service category2Service;
    
    /**
     * 获取所有一级分类
     */
    @GetMapping("/level1")
    public Map<String, Object> listCategory1() {
        List<FoodCategory1> categoryList = category1Service.list();
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", categoryList);
        return result;
    }
    
    /**
     * 获取所有二级分类
     */
    @GetMapping("/level2")
    public Map<String, Object> listCategory2(@RequestParam(required = false) Integer category1Id) {
        LambdaQueryWrapper<FoodCategory2> queryWrapper = new LambdaQueryWrapper<>();
        if (category1Id != null) {
            queryWrapper.eq(FoodCategory2::getCategory1Id, category1Id);
        }
        List<FoodCategory2> categoryList = category2Service.list(queryWrapper);
        
        // 查询每个二级分类的一级分类信息
        for (FoodCategory2 category2 : categoryList) {
            FoodCategory1 category1 = category1Service.getById(category2.getCategory1Id());
            category2.setFoodCategory1(category1);
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", categoryList);
        return result;
    }
    
    /**
     * 获取一级分类详情
     */
    @GetMapping("/level1/{id}")
    public Map<String, Object> getCategory1(@PathVariable Integer id) {
        FoodCategory1 category = category1Service.getById(id);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", category);
        return result;
    }
    
    /**
     * 获取二级分类详情
     */
    @GetMapping("/level2/{id}")
    public Map<String, Object> getCategory2(@PathVariable Integer id) {
        FoodCategory2 category = category2Service.getById(id);
        
        if (category != null) {
            FoodCategory1 category1 = category1Service.getById(category.getCategory1Id());
            category.setFoodCategory1(category1);
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", category);
        return result;
    }
    
    /**
     * 新增一级分类
     */
    @PostMapping("/level1")
    public Map<String, Object> saveCategory1(@RequestBody FoodCategory1 category) {
        boolean success = category1Service.save(category);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "添加成功");
            result.put("data", category);
        } else {
            result.put("code", 500);
            result.put("message", "添加失败");
        }
        return result;
    }
    
    /**
     * 新增二级分类
     */
    @PostMapping("/level2")
    public Map<String, Object> saveCategory2(@RequestBody FoodCategory2 category) {
        boolean success = category2Service.save(category);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "添加成功");
            result.put("data", category);
        } else {
            result.put("code", 500);
            result.put("message", "添加失败");
        }
        return result;
    }
    
    /**
     * 修改一级分类
     */
    @PutMapping("/level1")
    public Map<String, Object> updateCategory1(@RequestBody FoodCategory1 category) {
        boolean success = category1Service.updateById(category);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "修改成功");
        } else {
            result.put("code", 500);
            result.put("message", "修改失败");
        }
        return result;
    }
    
    /**
     * 修改二级分类
     */
    @PutMapping("/level2")
    public Map<String, Object> updateCategory2(@RequestBody FoodCategory2 category) {
        boolean success = category2Service.updateById(category);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "修改成功");
        } else {
            result.put("code", 500);
            result.put("message", "修改失败");
        }
        return result;
    }
    
    /**
     * 删除一级分类
     */
    @DeleteMapping("/level1/{id}")
    public Map<String, Object> removeCategory1(@PathVariable Integer id) {
        // 查询是否有关联的二级分类
        int count = (int) category2Service.count(new LambdaQueryWrapper<FoodCategory2>()
                .eq(FoodCategory2::getCategory1Id, id));
        if (count > 0) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("message", "该分类下有关联的二级分类，不能删除");
            return result;
        }
        
        boolean success = category1Service.removeById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }
        return result;
    }
    
    /**
     * 删除二级分类
     */
    @DeleteMapping("/level2/{id}")
    public Map<String, Object> removeCategory2(@PathVariable Integer id) {
        boolean success = category2Service.removeById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }
        return result;
    }
} 